package com.penglecode.dp.behavioral.template;

import org.springframework.util.Assert;
/**
 * 模板模式
 * 
 * @author pengpeng
 * @date 2013-6-9上午10:27:20
 * @version 1.0
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public abstract class CommonPay implements IPay {

	public PayResult processPay(PayParameter payParameter) {
		PayResult payResult = null;
		try {
			validateParameter(payParameter);
			logPayParameter(payParameter);
			payResult = doPay(payParameter);
		} finally {
			logPayResult(payResult);
		}
		return null;
	}
	
	/**
	 * 具体的支付核心操作迟延到子类中去
	 * @param payParameter
	 * @return
	 */
	protected abstract PayResult doPay(PayParameter payParameter);
	
	protected void validateParameter(PayParameter payParameter){
		Assert.notNull(payParameter);
		Assert.notEmpty(payParameter.getPaymentConfig());
		Assert.hasLength(payParameter.getSerialNo());
		Assert.hasLength(payParameter.getTradeNo());
		Assert.hasLength(payParameter.getTradeTime());
		Assert.state(payParameter.getTotalAmount() > 0);
	}
	
	protected void logPayParameter(PayParameter payParameter){
		//logging payParameter into database
	}
	
	protected void logPayResult(PayResult payResult){
		//logging payResult into database
	}
	
}
